package com.shuoyue.adven.init;


import com.shuoyue.adven.network.RetrofitClient;
import com.sm.appbase.base.basemvp.BaseModel;
import com.sm.appbase.base.basemvp.BasePresenter;
import com.sm.appbase.base.basemvp.BaseView;
import com.sm.appbase.net.base.ApiSubscriber;
import com.sm.appbase.net.base.BaseResult;
import com.sm.appbase.net.base.Optional;

import io.reactivex.Observable;
import lombok.AllArgsConstructor;
import lombok.Data;

public interface RegistContract {
    interface View extends BaseView {
        void registResult(String token);

    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void regist(String registCode, String password) {
            apply(model.regist(registCode, password))
                    .subscribe(new ApiSubscriber<Optional<RetistResult>>(mView, this) {
                        @Override
                        public void onNext(Optional<RetistResult> stringOptional) {
                            super.onNext(stringOptional);
                            mView.registResult(stringOptional.getIncludeNull().token);
                        }
                    });
        }
    }

    class Model extends BaseModel {
        public Observable<BaseResult<RetistResult>> regist(String registCode, String password) {
            return RetrofitClient.getInstance().getApi().registDevices(new RegistParams(registCode, password));
        }
    }

    @Data
    @AllArgsConstructor
    public static class RegistParams {
        String id;
        String password;
    }

    public static class RetistResult {
        String token;
    }
}
